<?php
namespace app\admin\controller;

use app\admin\model\Admin as AdminModel;
use app\admin\model\Agent as AgentModel;
use app\admin\model\AdminGroup as AdminGroupModel;
use think\Db;
use think\Request;

/**
 * 登录控制器
 * @package app\backend\controller
 */
class Login extends \think\Controller
{
    // 登录页面
    public function index()
    {
        $domain = $_SERVER['HTTP_HOST'];// . ':' . $_SERVER['SERVER_PORT'];
        $this->assign('domain', $domain);

        return $this->fetch();
    }

    // 执行登录
    public function login()
    {
        $req = \think\Request::instance();
        if ($req->isPost())
        {
            $type = $req->post("type");
            $username = $req->post("username");
            $password = md5($req->post('password') . "zhinengyunjing");
            if($type==2){
                $record = AgentModel::field('id,admin_group_id,password')->where('name' , $username)->find();
            }else if($type==1){
                $record = AdminModel::field('id,admin_group_id,password')->where('username' , $username)->find();
            }

            // $remember = $req->post('remember');
            $menu_id = AdminGroupModel::where('id' , $record['admin_group_id'])->value('menu_id');
            //$account_id = $record['account_id'];

            if (!$record || ($record['password'] != $password))
            {
                return json([
                    'code' => 4001,
                    'message'=>'用户名或者密码错误，请检查' //'用户名或者密码错误，请检查',
                ]);
            }
            /*if ($remember)
            {
                \think\Cookie::set('username', $username, 86400);
            }*/
            \think\Session::set('username', $username);
            \think\Session::set('menu_id', $menu_id);
            \think\Session::set('admin_id', $record['id']);

            // \think\Session::set('login_id', $record['id']);
            // \think\Session::set('auth', $record['auth']);
            //AuthAccess::access2Session($account_id);
            return json([
                'code' => 200,
                'result'=>$record,
                // 'username' => \think\Session::get('username'),
                // 'session' => $_SESSION,
                // 'session_id' => session_id()
            ]);
        }
    }

    // 退出
    public function logout()
    {
        \think\Session::clear();
        \think\Cookie::clear();
        \think\Cookie::delete('username');
        \think\Cookie::delete('PHPSESSID');
        $this->redirect('index');
    }

    /*public function resetpass(){
        $req = \think\Request::instance();
        if ($req->isPost())
        {
            $username = $req->post('username');
            $password = md5($req->post('password'));
            $pass1 = ($req->post('password1'));
            if(empty($pass1)){
                $this->error("密码不能为空");
            }
            $password1 = md5($req->post('password1'));
            $password2 = md5($req->post('password2'));
            $userpd2 = AccountModel::where(["id"=>2])->value("password");
            /*print_r($password);
            echo "<br>";
            print_r($userpd2);exit;*/
           /* if(!($password == $userpd2)){
                $this->error("原始密码输入错误");
            }
            if($password1 != $password2){
                $this->error("重置密码输入错误");
            }

            $user = \think\Db::name('account')->where(["account_id"=>2])->update(['username' => $username]);
            $pass = \think\Db::name('account')->where(["account_id"=>2])->update(['password' => $password1]);
            $this->success('修改成功',url('/admin/login/index'));
        }

        $model = new AccountModel;
        $pass = $model->getAll();
        //var_dump($pass);exit;
        $this->assign('user',$pass[0]);
        return $this->fetch();
    }*/
}